﻿/*
 * ========================================================================
 * Copyright(c) 2008-2010北京高阳金信信息技术有限公司, All Rights Reserved.
 * ========================================================================
 *  资讯解读数据处理
 * 
 * 作者：常晏玮     时间：2010-06-09
 * 版本：V1.0
 * 
 * 修改者：         时间：                
 * 修改说明：
 * ========================================================================
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NBFDAL.SqlMapDAL;
using NBFModel;
using PWMIS.DataMap.Entity;

namespace NBFBLL
{

    public class InfoReadBLL
    {
        private string _articleurl = "http://192.168.50.17:8085/web/article.aspx";
        /// <summary>
        /// 文章URL
        /// </summary>
        public string ArticleURL
        {
            get { return _articleurl; }
        }


        /// <summary>
        /// 获取文章标题
        /// </summary>
        /// <param name="ArticleID">文章ID</param>
        /// <returns>文章标题</returns>
        public string GetArticleTitle(int ArticleID)
        {
            InfoRead inforead = new InfoRead();
            return (string)inforead.GetArticleTitle(ArticleID);
        }

        /// <summary>
        /// 获取文章标题列表
        /// </summary>
        /// <param name="ArticleID">条件</param>
        /// <returns>文章标题数据表</returns>
        public List<Query_Tb_Sys_Article> GetArticleTitleList1(string wherestring)
        {
            InfoRead inforead = new InfoRead();
            return inforead.GetArticleTitleList1(wherestring);
        }

        public List<Tb_Sys_Article> GetArticleTitleList2(string wherestring)
        {
            InfoRead inforead = new InfoRead();
            return inforead.GetArticleTitleList2(wherestring);
        }


        /// <summary>
        /// 获取客户名称与Email列表
        /// </summary>
        /// <param name="ArticleID">用户分组ID,如果groupid小于1则返回全部用户</param>
        /// <returns>文章标题数据表</returns>
        public List<Query_tb_zdgl_users> GetCustomerEmailList(int groupid)
        {
            if (groupid > 0)
            {
                InfoRead inforead = new InfoRead();
                return inforead.GetCustomerEmailListByGroupID(groupid);
            }
            else
            {
                InfoRead inforead=new InfoRead();
                return inforead.GetCustomerEmailList();
            }
        }


        /// <summary>
        /// 提交评论
        /// </summary>
        /// <param name="ArticleID">文章ID</param>
        /// <param name="Content">评论内容</param>
        /// <param name="name">理财师姓名,可以是"匿名"</param>
        /// <param name="ReadType">阅读权限1私人可见2同行可见3全部银行可见</param>
        /// <param name="ProductNumbers">产品序列号</param>
        /// <returns>成功为真,否则为假</returns>
        public bool PostReview(int ArticleID, string Content, string name, int ReadType, string ProductNumbers)
        {
            Tb_Sys_ArticleRemark ar = new Tb_Sys_ArticleRemark();
            EntityQuery<Tb_Sys_ArticleRemark> query = new EntityQuery<Tb_Sys_ArticleRemark>(ar, true);
            ar.RemarkID = Guid.NewGuid().ToString();
            ar.ArticleID = ArticleID;
            ar.Content = Content;
            ar.ProductNumbers = ProductNumbers;
            ar.name = name;
            ar.ReadType = ReadType.ToString();
            ar.State = (ReadType == 1 ? 1 : 0).ToString();
            ar.Addtime = DateTime.Now;

            try
            {
                query.Save();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 添加到我的收藏
        /// </summary>
        /// <param name="ArticleID">文章ID</param>
        /// <returns>成功返回0,失败：已存在返回1,出错返回-1</returns>
        public int AddMyFavorites(int ArticleID)
        {
            int recount = 0;
            try
            {
                InfoRead ir = new InfoRead();
                string t=(string)ir.GetArticleCollectCount(ArticleID);
                if (false == Int32.TryParse(t, out recount))
                {
                    recount = 0;
                }
            }
            catch(Exception ex)
            {
                recount = -1;
            }

            if (recount > 0) return 1;
            
            Tb_Sys_ArticleCollect ac = new Tb_Sys_ArticleCollect();
            EntityQuery<Tb_Sys_ArticleCollect> query = new EntityQuery<Tb_Sys_ArticleCollect>(ac, true);
            ac.ArticleID = ArticleID;
            ac.addTime = DateTime.Now;

            try
            {
                query.Save();
            }
            catch (Exception ex)
            {
                return -1;
            }
            return 0;    
        }

        /// <summary>
        /// 填加到已发送邮件
        /// </summary>
        /// <param name="ArticleID"></param>
        /// <returns></returns>
        public bool AddArticleSend(string BodyHtml, ArrayList ReceiveMan, string mailtitle)
        {
            StringBuilder reman = new StringBuilder(100);
            for (int i = 0; i < ReceiveMan.Count; i++)
            {
                reman.Append(ReceiveMan[i].ToString());
                reman.Append(",");
            }
            if (reman.Length > 0) reman = reman.Remove(reman.Length-1,1);
            Tb_Sys_ArticleSend tsas = new Tb_Sys_ArticleSend();
            EntityQuery<Tb_Sys_ArticleSend> query = new EntityQuery<Tb_Sys_ArticleSend>(tsas, true);
            tsas.SendID = Guid.NewGuid().ToString();
            tsas.Content = BodyHtml;
            tsas.ReceiveMan = reman.ToString();
            tsas.addTime = DateTime.Now.ToString();
            tsas.mailSubject = mailtitle;


            try
            {
                query.Save();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }


        /// <summary>
        /// 本行策略获取文章标题列表
        /// </summary>
        /// <param name="wheres">条件</param>
        /// <returns>文章标题数据表</returns>
        public List<Query_FT_Compact_Strategy> GetBankStrategy(string wherestring, string ci_id)
        {
            InfoRead ir = new InfoRead();
            return ir.GetBankStrategy(ci_id, wherestring);
        }
    }
}
